<?php

namespace App\Http\Controllers;

use App\Services\ArticleService;
use App\Services\SystemService;
use App\Services\VisitService;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    protected $_articleService;
    protected $_systemService;
    protected $_visitService;

    public function __construct()
    {
        if (!$this->_articleService) {
            $this->_articleService = new ArticleService();
        }
        if (!$this->_systemService) {
            $this->_systemService = new SystemService();
        }
        if (!$this->_visitService) {
            $this->_visitService = new VisitService();
        }
    }

    public function layout_view($view, $params)
    {
        $list = $this->_systemService->getNavigationList();
        $data['list'] = $list;
        $data['view'] = $view;
        $data['name'] = '叶宗调';
        $data['params'] = $params;
        $special_recommend = $this->_articleService->getArticleRecommend($this->_articleService::RECOMMEND_TYPE_SPECIALLY);
        $normal_recommend = $this->_articleService->getArticleRecommend($this->_articleService::RECOMMEND_TYPE_NORMAL);
        $data['specially_recommend_list'] = $special_recommend;
        $data['normal_recommend_list'] = $normal_recommend;
        $data['article_top_list'] = $this->_articleService->getArticleTitleTop('main');
        $data['tag_list'] = $this->_systemService->getParams($this->_systemService::PARAMS_TYPE_TAG, true);
        return view('common/layout', $data);
    }

    public function show_result($error, $data)
    {
        return array('error' => $error, 'data' => $data);
    }
    public function success($data,$msg='')
    {
        return json_encode(['error'=>0,'msg'=>$msg,'data'=>$data]);
    }
}
